package com.hblack.manage.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hblack.common.bean.EasyUIResult;
import com.hblack.manage.mapper.ItemMapper;
import com.hblack.manage.mapper.ItemParamItemMapper;
import com.hblack.manage.pojo.Item;
import com.hblack.manage.pojo.ItemDesc;
import com.hblack.manage.pojo.ItemParamItem;

@Service
public class ItemService extends BaseService<Item>{

	@Autowired
	private ItemDescService itemDescService;
	@Autowired
	private ItemMapper itemMapper;
	@Autowired
	private ItemParamItemService itemParamItemService;
	
	/**
	 * 添加商品
	 * @param item
	 * @param desc
	 * @param itemParams 
	 * @return
	 */
	public Boolean saveItem(Item item, String desc, String paramData){
		
		item.setStatus(1);
		item.setId(null);
		
		Integer itemCount = save(item);
		if(itemCount != 1){
			throw new RuntimeException("添加商品失败，但是没有异常,item:" + item);
		}
		
		//添加商品描述
		ItemDesc itemDesc = new ItemDesc();
		itemDesc.setItemDesc(desc);
		itemDesc.setItemId(item.getId());
		
		Integer itemDescCount = this.itemDescService.save(itemDesc);
		if(itemDescCount != 1){
			throw new RuntimeException("添加商品描述信息失败，但是没有异常,item:" + item + "itemDesc:" + itemDesc);
		}
		
		//添加商品规格参数
		ItemParamItem itemParamItem = new ItemParamItem();
		itemParamItem.setItemId(item.getId());
		itemParamItem.setParamData(paramData);
		
		int itemParamsItemCount = itemParamItemService.save(itemParamItem);
		if(itemParamsItemCount != 1){
			throw new RuntimeException("添加商品规格参数失败，但是没有异常,item:" + item + "itemDesc:" + itemDesc + "itemParamItem:" + itemParamItem);
		}
		
		return true;
	}

	/**
	 * 分页查询商品
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public EasyUIResult  queryPage(Integer pageNum, Integer pageSize){
		
		PageHelper.startPage(pageNum, pageSize);
		
		Example example = new Example(Item.class);
		example.setOrderByClause("updated desc");
		List<Item> list = itemMapper.selectByExample(example);
		
		PageInfo<Item> pageInfo = new PageInfo<Item>(list);
		
		return new EasyUIResult(pageInfo.getTotal(), list);
	}

	/**
	 * 更新商品以及对应的商品描述，商品规格参数
	 * @param item
	 * @param itemDesc
	 * @param params
	 * @param itemParamId
	 * @return
	 */
	public Boolean updateItemByItemId(Item item, String desc,
			String params, Long itemParamId) {
		
		item.setStatus(null);
		Integer itemCount = this.updateByPrimaryKey(item);
		if(itemCount != 1) {
			throw new RuntimeException("更新商品失败,Item:" + item);
		}
		
		ItemDesc itemDesc = new ItemDesc();
		itemDesc.setItemId(item.getId());
		itemDesc.setItemDesc(desc);
		Integer descCount = this.itemDescService.updateByPrimaryKey(itemDesc);
		if(descCount != 1) {
			throw new RuntimeException("更新商品描述失败,Item:" + item + "ItemDesc:" + itemDesc);
		}
		
		
		ItemParamItem itemParamItem = new ItemParamItem();
		itemParamItem.setId(itemParamId);
		itemParamItem.setParamData(params);
		Integer paramsCount = this.itemParamItemService.updateByPrimaryKey(itemParamItem);
		if(paramsCount != 1) {
			throw new RuntimeException("更新商品规格参数失败,Item:" + item + "ItemDesc:" + itemDesc + "ItemParamItem:" + itemParamItem);
		}
		
		return true;
	}
}
